﻿using CNKI.TPI.Web.Base;
using CNKI.TPI.Web.Search.IDAO;
using CNKI.TPI.Web.Search.Model;
using KNet.Data.Entity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace CNKI.TPI.Web.Search.DAO
{
    public class CommonDAO : BaseDAO, ICommonDAO
    {
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="match"></param>
        /// <returns></returns>
        public IList<T> Get<T>(Expression<Func<T, bool>> match, Expression<Func<T, bool>> order = null) where T : BaseModel, IView, new()
        {
            return Context.Get<T>(match, order);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="match"></param>
        /// <returns></returns>
        public IList<T> GetAll<T>(Expression<Func<T, bool>> order = null) where T : BaseModel, IView, new()
        {
            return Context.Get<T>(null, order);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public bool ExecuteNonQuery(string sql)
        {
            bool result = false;
            try
            {
                if (ConfigHelper.IsShowSQLEnabled())
                {
                    LogOpr.Debug(sql);
                }
                Context.ExecuteNonQuery(sql);
                result = true;
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public DataTable ExecuteSql(string sql)
        {
            DataTable result = null;
            try
            {
                if (ConfigHelper.IsShowSQLEnabled())
                {
                    LogOpr.Debug(sql);
                }
                result = Context.ExecuteSql(sql);
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
                result = new DataTable();
            }
            return result;
        }
    }
}
